package com.baitiaojun.api.vo;

import com.baitiaojun.api.util.ConstantUtil;
import lombok.*;

import java.io.Serializable;
import java.util.HashMap;

/**
 * @类名 Res
 * @描述
 * @作者 白条君
 * @创建日期 2022/10/25 00:01
 * @版本 1.0
 */
@ToString
public class JsonRes extends HashMap<String, Object> implements Serializable {

    private static final long serialVersionUID = -1L;

    private JsonRes() {}

    public static JsonRes builder() {
        return new JsonRes();
    }

    public <T> JsonRes data(T data) {
        put("data", data);
        return this;
    }

    public static class Error extends JsonRes {

        private static final long serialVersionUID = -2436583441019209378L;

        Error() {
            put("status", ConstantUtil.status.ERROR.getStatus());
            put("msg", "fail");
        }

        Error(Integer status, String msg) {
            put("status", status);
            put("msg", msg);
        }

        public <T> Error data(T data) {
            put("data", data);
            return this;
        }
    }

    public static class Success extends JsonRes {

        private static final long serialVersionUID = 338806363710704317L;

        Success() {
            put("status", ConstantUtil.status.SUCCESS.getStatus());
            put("msg", "success");
        }

        Success(Integer status, String msg) {
            put("status", status);
            put("msg", msg);
        }

        public <T> Success data(T data) {
            put("data", data);
            return this;
        }
    }

    public Success success() {
        return new JsonRes.Success();
    }

    public Success success(Integer status, String msg) {
        return new JsonRes.Success(status, msg);
    }

    public Error error() {
        return new JsonRes.Error();
    }

    public Error error(Integer status, String msg) {
        return new JsonRes.Error(status, msg);
    }
}